package com.naizhuai.ui.mianbusiness.action.presenter;

import android.os.Environment;

import com.naizhuai.AppContext;
import com.naizhuai.network.DownloadFileListener;
import com.naizhuai.network.RetrofitServerFactory;
import com.naizhuai.network.apistore.DownloadImageRequest;
import com.naizhuai.util.NaizhuAILog;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.ResponseBody;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 处理公共搜索的类
 * Created by code_han on 2018/1/25.
 */

public class DownloadImageApiStoreIml implements DownloadImageApiStore {

    private static DownloadImageApiStore INSTANCE = null;

    public static DownloadImageApiStore getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new DownloadImageApiStoreIml();
        }
        return INSTANCE;
    }

    /**
     * 下载图片到指定的路径
     *
     * @param ImageUrl 下载文件的url
     * @param filePath 需要下载的指定的路径
     * @return
     */
    @Override
    public Observable<File> downloadImageByUrl(String ImageUrl, String filePath) {
        return createBaseClass().downloadImageByUrl(ImageUrl).subscribeOn(Schedulers.io()).map(new Func1<ResponseBody, File>() {
            @Override
            public File call(ResponseBody responseBody) {
                return writeResponseBodyToDisk(responseBody, filePath);
            }
        }).observeOn(AndroidSchedulers.mainThread());
    }

    private File writeResponseBodyToDisk(ResponseBody body,String filePath) {
        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File(filePath);

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }
                    outputStream.write(fileReader, 0, read);
                }

                outputStream.flush();

                return futureStudioIconFile;
            } catch (IOException e) {
                return null;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return null;
        }
    }

    private DownloadImageRequest createBaseClass() {
        return RetrofitServerFactory.createDownload(AppContext.getInstance(), DownloadImageRequest.class);
    }

}
